/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__hash_node_ct_run.c                              */
/*  NOTE      =                                                    */
/*  DATE      = 2015/02/02 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frsutl.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "module_comm.h"
#include "module.h"
#include "frstat_topology.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

int __hash_node_ct_run(Frlist_node* p_st_node, const void *p_data);

void frstat_topology_destroy(Frhash_header *p_st_list_header);
void frstat_topology_output(Frhash_header *p_st_list_header, void *socket_id);

void mylist_node_free(Frlist_node *p_st_node)
{
    L7_analyzer *p_l7analyzer = NULL;
    if(p_st_node == NULL)
        return;

    if(p_st_node->data != NULL)
    {
        p_l7analyzer = (L7_analyzer *)p_st_node->data;
        if(p_l7analyzer->p_st_l7_module != NULL)
            frfree(p_l7analyzer->p_st_l7_module);

        frfree(p_l7analyzer);
    }
    return;
}



static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_ct = NULL;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));

    pthread_spin_init(&p_st_ct->lock, 0);
    i_ret = __hash_node_ct_run(NULL,NULL);
    pthread_spin_destroy(&p_st_ct->lock);

    CU_ASSERT_EQUAL(i_ret,1);

    frfree(p_st_ct);

    return;

}

static void case2(void)
{
    int i_ret = 0;
    L7_module *p_st_l7_module = NULL;
    L7_analyzer *p_l7analyzer = NULL;
    Conntrack *p_st_ct = NULL;
    Frlist_node *p_l7_node = NULL;
    Frlist_node *p_st_node = NULL;
    Frhash_header st_hash_topology;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));

    p_st_ct->src_ip = iptools_parse_ipaddr("192.168.1.1");
    p_st_ct->dst_ip = iptools_parse_ipaddr("192.168.1.1");
    p_st_ct->dst_port = 80;

    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    memset(p_st_l7_module, 0x00, sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1433;

    p_l7analyzer = &p_st_ct->st_l7_analyzer;
    memset(p_l7analyzer, 0x00, sizeof(L7_analyzer));
    p_l7analyzer->p_st_l7_module = p_st_l7_module;


    p_st_node = frlist_node_create(p_st_ct);

    frhash2_create(&st_hash_topology,100);
    pthread_spin_init(&p_st_ct->lock, 0);
    i_ret = __hash_node_ct_run(p_st_node,&st_hash_topology);
    pthread_spin_destroy(&p_st_ct->lock);

    CU_ASSERT_EQUAL(i_ret,1);

    frstat_topology_output(&st_hash_topology, NULL);
    frstat_topology_destroy(&st_hash_topology);
    frfree(p_st_l7_module);
    frfree(p_st_ct);
    frfree(p_st_node);

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__hash_node_ct_run 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}